<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('申请标题')" prop="actTitle">
            <el-input v-model="form.actTitle" show-word-limit max-length="100" :placeholder="$t('申请标题')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('基金简称')" prop="fundId">
            <main-body-select
              v-model="form.fundId"
              parameters="fund"
              width="100%"
              :placeholder="$t('基金简称')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('年度')" prop="year">
            <el-date-picker
              v-model="form.year"
              type="year"
              value-format="yyyy"
              :placeholder="$t('年度')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('季度')" prop="quarterly">
            <dict-select
              v-model="form.quarterly"
              dictionary="sys_quarter"
              style="width: 100%"
              :placeholder="$t('季度')"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <FormBtn ref="formBtn" has-permi="fund:monitor" :bus-data="form" :btn-hide="btnHide" :menu-key="menuKey" />
    </div>
  </div>
</template>

<script>
    import { saveFundProductOperationMonitor, getFundProductOperationMonitor, checkReportDoesItExist } from '@/api/fund/operation/fundProductOperationMonitor'
    import { getByMonitorId } from '@/api/fund/operation/fundProductInfo'
    import MainBodySelect from '@/components/MainBodySelect'
    export default {
      components: {
        MainBodySelect
      },
      data() {
        return {
          // 菜单标识
          menuKey: 'fundProductOperationMonitor',
          // 表单参数
          form: {},
          // 遮罩层
          loading: false,
          // 是否隐藏保存按钮
          btnHide: false,
          // 表单校验
          rules: {
            actTitle: [
              { required: true, message: this.$t('必填信息'), trigger: 'blur' }
            ],
            fundId: [
                { required: true, message: this.$t('必填信息'), trigger: 'blur' }
            ],
            year: [
              { required: true, message: this.$t('必填信息'), trigger: 'blur' }
            ],
            quarterly: [
              { required: true, message: this.$t('必填信息'), trigger: 'blur' }
            ]
          },
          oldData: {}
        }
      },
      created() {
      },
      methods: {
        // 初始化数据
        init(data) {
          this.loading = true
          if (data != null && data.id !== undefined) {
            getFundProductOperationMonitor(data.id).then(response => {
              this.form = response.data
              this.oldData = { actTitle: this.form.actTitle, id: this.form.id, fundId: this.form.fundId, year: this.form.year, quarterly: this.form.quarterly }
              this.loading = false
            })
          } else {
            this.reset()
            this.loading = false
          }
        },
        // 表单重置
        reset() {
          this.form = {
            id: undefined,
            fundId: undefined,
            year: undefined,
            quarterly: undefined,
            delFlag: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined
          }
          this.resetForm('form')
        },
        // 取消按钮
        cancel() {
          this.$parent.$parent.formOpen = false
          this.$parent.$parent.indicatorFormOpen = false
        },
        // 关闭form页面
        closeForm() {
          // 关闭form页面遮罩层
          this.loading = false
          // 关闭form页面
          if (this.$parent.$parent.formOpen !== undefined) {
            this.$parent.$parent.formOpen = false
          }
          if (this.$parent.$parent.indicatorFormOpen !== undefined) {
            this.$parent.$parent.indicatorFormOpen = false
          }
          // 刷新list页面
          if (this.$parent.$parent.getList !== undefined) {
            this.$parent.$parent.getList()
          }
        },
        /** 提交按钮 */
        submitForm: function(type) {
          return new Promise((resolve, reject) => {
            this.$refs['form'].validate(valid => {
              if (valid) {
                this.loading = true
                if (this.oldData.actTitle === this.form.actTitle && this.oldData.id === this.form.id && this.oldData.fundId === this.form.fundId && this.oldData.year === this.form.year && this.oldData.quarterly === this.form.quarterly) {
                  if (type === 'process') {
                    this.startProcess(this.form.id)
                  } else {
                    this.closeForm()
                  }
                  resolve()
                } else {
                  this.changeFund(type)
                  resolve()
                }
              } else {
                reject()
                this.locationError(document)
              }
            })
          })
        },
        submitFormData(type) {
          saveFundProductOperationMonitor(this.form).then(response => {
            if (response.code === 200) {
              if (type === 'process') {
                // 设置数据id方式启动流程后取消并再次启动会创建多条记录
                this.form.id = response.data.id
                // 启动流程
                this.startProcess(response.data.id)
              } else {
                this.msgSuccess(this.$t('保存成功'))
                this.closeForm()
              }
            } else {
              this.msgError(response.msg)
              this.loading = false
            }
            this.loading = false
          })
        },
        /**
        * 校验该基金的报告期是否存在了
        */
        changeFund(type) {
          if (this.form.id) {
            this.submitFormData(type)
          } else {
            checkReportDoesItExist(this.form.fundId, this.form.year, this.form.quarterly).then(res => {
              if (res.code === 200) {
                if (res.data !== null && res.data.length > 0) {
                  this.msgError('当前年度,季度中该基金已存在,请检查!')
                } else {
                  this.submitFormData(type)
                }
              }
              this.loading = false
            })
          }
        },
        // 启动流程
        startProcess: async function(dataId) {
          const flag = await getByMonitorId(this.form.id).then(res => {
            if (!res.data) {
              this.$message({
                message: this.$t('请先填写指标！'),
                type: 'warning'
              })
              return false
            } else {
              return true
            }
          })
          this.loading = false
          if (!flag) {
            return
          }
          // 启动流程时候传入的参数
          const processData = {}
          // 待办标题
          processData.title = '[' + this.$t('产品运行监测表') + ']: ' + this.form.actTitle
          // 业务表名
          processData.dataTable = 'fund_product_operation_monitor'
          // 业务ID
          processData.dataId = dataId
          // 业务表单地址
          processData.formPath = '@/views/fund/operation/fundProductOperationMonitorHome.vue'

          // 业务表单打印地址
          processData.viewPath = ''

          // 菜单标识(根据菜单标识判断的走哪个流程)
          processData.menuKey = this.menuKey
          this.startProcessByMenu(processData, () => {
            this.form.actStatus = 'D'
            this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
            this.closeForm()
          })
        }
      }
    }
</script>
